package com.hx.myfristandriod;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

public class TestMenuActivity extends Activity {
 
private MenuInflater mif;
private  TextView  txtShow;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       setContentView(R.layout.test_menu);
       //实列菜单对象
    //   mif=new MenuInflater(this);
       txtShow=(TextView) findViewById(R.id.menuTxtView01);
      registerForContextMenu(txtShow);
    }

   //创建菜单项
    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
    	mif.inflate(R.menu.file_menu, menu);
        return true;
    }

    //响应菜单单击事件，当单击about显示帮助，。当单击exit退出程序
    @Override
	public boolean onOptionsItemSelected(MenuItem item) {
       
     switch (item.getItemId()) {
				case R.id.about:
					aboutAlert("本实列是演示如何使用xml菜单资源定义菜单");
					break;
				case R.id.exit:
					exitAlert("真的要退出吗？");
					break;
				default:
					break;
				   }
     return true;
    }

    //显示对话框
    private  void aboutAlert(String msg){
     //实列dialog
    	AlertDialog.Builder bd=new AlertDialog.Builder(this);
    	//设置显示信息
    	bd.setMessage(msg)
    	.setCancelable(false)
    	.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
			
			}
		});
    	//创建对话框
    	AlertDialog alert=bd.create();
    	alert.show();
    }
    
    private void  exitAlert(String msg){
    	 //实列dialog
    	AlertDialog.Builder bd=new AlertDialog.Builder(this);
    	//设置显示信息
    	bd.setMessage(msg).setCancelable(false)
    	.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
			 finish();//结束activity
			}
		}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
			}
		});
    	AlertDialog alert=bd.create();
    	alert.show();
    }
    
    
    //重写一个上下文菜单
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		menu.setHeaderTitle("请选择");
		menu.setHeaderIcon(android.R.drawable.ic_menu_more);
	     this.getMenuInflater().inflate(R.menu.file2_menu, menu);
		super.onCreateContextMenu(menu, v, menuInfo);
	}

	//菜单点击事件
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_1:
		System.out.println("功能1");
			break;
		case R.id.menu_2:
			System.out.println("功能2");
			break;
		default:
			break;
		   }
		return true;	
	}

}
